import shopAxios from '../utils/request.js'

const BASE_URL = '/goods'

// 添加商品 返回 id
export function add(goods) {
  return shopAxios({
    method: 'post',
    url: `${BASE_URL}/add`,
    data: goods
  })
}

// 更新商品
export function update(goods) {
  return shopAxios({
    method: 'post',
    url: `${BASE_URL}/update`,
    data: goods
  })
}

// 获取商品详情
export function getDetail(id) {
  return shopAxios({
    method: 'get',
    url: `${BASE_URL}/getGoods`,
    params: { id }
  })
}

// 获取所有商品
export function getGoodsListAll() {
  return shopAxios({
    method: 'get',
    url: `${BASE_URL}/getGoodsListAll`,
  })
}

// 
export function getPageDivision(currentPage, pageSize) {
  return shopAxios({
    method: 'get',
    url: `${BASE_URL}/getPageDivision`,
    params: { currentPage, pageSize }
  })
}

// 删除商品
export function delGoods(id) {
  return shopAxios({
    method: 'post',
    url: `${BASE_URL}/delGoods`,
    data: { id }
  })
}
import { IMG_MAX_SIZE } from '../constants/File.js'
import { ElMessage } from 'element-plus'
// 文件上传前判断
function beforeUpload(file) {
  if (file.size > IMG_MAX_SIZE) {
    ElMessage.error('文件太大，请重新上传文件')
    return false
  }
  return true
}


// 上传商品图片
export function submitPic(goodsPic) {
  if (!beforeUpload(goodsPic)) return
  return shopAxios({
    method: 'post',
    url: `${BASE_URL}/submitPic`,
    data: goodsPic,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  })
}